package com.eveinfo.display.certificates;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.R;
import com.eveinfo.api.character.sheet.ApiSheet;
import com.eveinfo.api.utils.certificates.ApiCertificate;
import com.eveinfo.api.utils.skilllist.ApiSkills;
import com.eveinfo.display.LayoutUtils;
import com.eveinfo.tools.Tools;

public class CertificateViewRenderer extends TableLayout {

	private TextView lblName;
	private TextView lblTime;
	private ImageView lblStatut;
	private ImageView lblType;

	private Context context;

	public CertificateViewRenderer(Context context) {
		super(context);
		this.context = context;
		createUI();
	}

	/** create the UI components */
	private void createUI() {

		TableRow row = new TableRow(context);
		row.setPadding(0, 0, 10, 0);
		LayoutUtils.Layout.WidthFill_HeightWrap.applyTableLayoutParams(row);

		LinearLayout layout = new LinearLayout(context);
		layout.setGravity(Gravity.LEFT);
		lblType = new ImageView(context);
		lblType.setPadding(0, 0, 0, 0);
		layout.addView(lblType);

		LinearLayout layout2 = new LinearLayout(context);
		layout2.setPadding(5, 8, 0, 0);
		layout2.setGravity(Gravity.LEFT);
		lblStatut = new ImageView(context);
		layout2.addView(lblStatut);

		LinearLayout layout3 = new LinearLayout(context);
		layout3.setPadding(5, 0, 0, 0);
		layout3.setGravity(Gravity.LEFT);
		layout3.setOrientation(LinearLayout.VERTICAL);
		lblName = new TextView(context);
		lblName.setTextColor(Color.WHITE);
		lblName.setGravity(Gravity.LEFT);
		LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableRowParams(lblName);
		lblTime = new TextView(context);
		lblTime.setTextColor(Color.WHITE);
		lblTime.setGravity(Gravity.LEFT);
		lblTime.setPadding(10, 0, 0, 0);
		LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableRowParams(lblTime);
		layout3.addView(lblName);
		layout3.addView(lblTime);

		row.addView(layout);
		row.addView(layout2);
		row.addView(layout3);

		addView(row);

	}

	public void displaySkill(int typeID, int level, ApiSheet sheet) {

		int skillLevel = sheet.getSkillLevel(typeID);

		ApiSkills allSkill = EveInfo.getData().getSkills();
		lblTime.setText(Tools.convertMinutesToTime(Tools.getTimeToLevel(sheet,
				allSkill, allSkill.getSkill(typeID), level, true)));
		lblTime.setTextSize(12);
		lblName.setText(allSkill.getSkillName(typeID) + " " + level);
		lblName.setTextSize(14);
		lblType.setImageResource(R.drawable.icon06_01);
		if (skillLevel == -1)
			lblStatut.setImageResource(R.drawable.red);
		if (level > skillLevel)
			lblStatut.setImageResource(R.drawable.orange);
		else
			lblStatut.setImageResource(R.drawable.green);

		return;
	}

	public void displayCertificate(ApiCertificate certificate, ApiSheet sheet) {
		ApiSkills allSkill = EveInfo.getData().getSkills();
		lblTime.setText(Tools.convertMinutesToTime(Tools.getTimeToLevel(sheet, allSkill, certificate, true)));
		lblTime.setTextSize(12);
		lblName.setText(certificate.getName() + " " + Tools.gradeName(certificate.getGrade()));
		lblName.setTextSize(14);
		lblType.setImageResource(R.drawable.icon79_01_32);
		if (sheet.haveCertificate(certificate.getID()))
			lblStatut.setImageResource(R.drawable.green);
		else
			lblStatut.setImageResource(R.drawable.red);
		return;
	}

	public void displayCertificateHeader(ApiCertificate certificate, ApiSheet sheet) {
		ApiSkills allSkill = EveInfo.getData().getSkills();
		lblTime.setText(Tools.convertMinutesToTime(Tools.getTimeToLevel(sheet, allSkill, certificate, true)));
		lblTime.setTextSize(12);
		lblName.setText(certificate.getName() + " " + Tools.gradeName(certificate.getGrade()));
		lblName.setTextSize(20);
		if (sheet.haveCertificate(certificate.getID()))
			lblStatut.setImageResource(R.drawable.green);
		else
			lblStatut.setImageResource(R.drawable.red);
		return;
	}
}
